package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.mapper.EduTeacherMapper;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-02
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    //1。分页查询讲师方法
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> pageParam) {
        //this.page()或者 baseMapper.selectPage()
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");//根据讲师id 做降序排列
        //把分页数据，封装到pageTeacher对象里面
        baseMapper.selectPage(pageParam,wrapper);//controller通过，service再传到这里的，分页对象（对象，条件）
        List<EduTeacher> records = pageParam.getRecords();//每页数据的list集合
        long current = pageParam.getCurrent();//当前页
        long pages = pageParam.getPages();//页数
        long size = pageParam.getSize();//每页记录数
       long total = pageParam.getTotal();//总记录数

        boolean hasNext = pageParam.hasNext();//下一页（判断当前是否有下一页）
        boolean hasPrevious = pageParam.hasPrevious();//上一页（判断当前是否，有上一页）

        //分页数据取出来，放到map里面
        Map<String,Object> map = new HashMap<>();//再放入数据
        map.put("items", records);//把取到的数据放到map里面
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);//(把数据取出，放到map集合中，再return（controller）)在前端取值
        //map返回
        return map;
    }
}
